<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Change_model extends CI_Model {

	function __construct() {
		parent::__construct();
		
	}
	
	/**
	 * 패스워드 일치여부 체크
	 */
	function check_password($user_id, $current_pwd) {
		log_message('debug',"#### Change_model::check_password($user_id, $current_pwd)");

		$this->db->select('pwd');
		$query = $this->db->get_where('tb_member', array('user_id'=>$user_id, 'use_yn'=>'Y')) or $this->logsql->log_sql('error');
		$result = $query->row_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		if ($result['pwd'] == $current_pwd) {
			return TRUE;
		}

		return FALSE;		
	}
	
	/**
	 * 패스워드 업데이트
	 */
	function update_member_password($user_id, $new_pwd) {
		log_message('debug',"#### Change_model::update_member_password($user_id, $new_pwd)");
		
		$this->db->set('pwd',$new_pwd);
		$this->db->where('user_id',$user_id);
		$this->db->where('use_yn','Y');
		$this->db->update('tb_member') or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
	}
	
	/**
	 * 탈퇴상태로 변경
	 */
	function update_member_dropout($user_id) {
		log_message('debug',"#### Change_model::update_member_dropout($user_id)");
		
		$this->db->set('use_yn','N');
		$this->db->set('ipin_di','');
		$this->db->set('telephone','');
		$this->db->set('cellphone','');
		$this->db->set('zip_code','');
		$this->db->set('addr1','');
		$this->db->set('addr2','');
		$this->db->where('user_id',$user_id);
		$this->db->where('use_yn','Y');
		$this->db->update('tb_member') or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
	}
	
	/**
	 * 회원정보 가져오기
	 */
	function get_member($user_id) {
		log_message('debug',"#### Change_model::get_member($user_id)");

		$query = $this->db->get_where('tb_member', array('user_id'=>$user_id, 'use_yn'=>'Y')) or $this->logsql->log_sql('error');
		$result = $query->row_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 닉네임 중복 체크
	 */	
	function is_already_nickname($user_id, $nickname) {
		log_message('debug',"#### Change_model::is_already_nickname($user_id, $nickname)");

		$this->db->where('user_id <>', $user_id);
		$this->db->where('nickname', $nickname);
		$this->db->where('use_yn', 'Y');
		$this->db->from('tb_member');
		$result = $this->db->count_all_results();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		if ($result > 0) {
			return TRUE;
		}
        return FALSE;
	}
	
	/**
	 * 이메일 중복 체크
	 */	
	function is_already_email($user_id, $email) {
		log_message('debug',"#### Change_model::is_already_email($user_id, $email)");

		$this->db->where('user_id <>', $user_id);
		$this->db->where('email', $email);
		$this->db->where('use_yn', 'Y');
		$this->db->from('tb_member');
		$result = $this->db->count_all_results();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		if ($result > 0) {
			return TRUE;
		}
        return FALSE;
	}
	
	/**
	 * 회원 정보 업데이트
	 */
	function update_member($user_id, $member, $result_upload) {
		log_message('debug', "#### Change_model::update_member($user_id, $member, $result_upload)");
			
		// 회원정보 입력
		$this->db->set($member);
		$this->db->set('update_dt', 'now()', FALSE);
		$this->db->where('user_id', $user_id);
		$this->db->update('tb_member') or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
		
		// 첨부파일이 있으면 기존 파일 삭제 후 파일정보 저장
		if (is_array($result_upload)) {
			$this->delete_member_file($member['idx']);
			$this->insert_member_file($member['idx'], $user_id, $result_upload);
		}
	}

	/**
	 * 파일 저장
	 */
	function insert_member_file($idx, $user_id, $result_upload) {
		log_message('debug',"#### Change_model::insert_member_file($idx, $user_id, $result_upload)");

		$this->db->set('idx'           , $idx);
		$this->db->set('user_id'       , $user_id);
		$this->db->set('file_name'     , $result_upload['file_name']);
		$this->db->set('file_type'     , $result_upload['file_type']);
		$this->db->set('file_path'     , $result_upload['file_path']);
		$this->db->set('file_url'      , substr($result_upload['file_path'], strpos($result_upload['file_path'],'/uploads',0), strlen($result_upload['file_path'])));
		$this->db->set('raw_name'      , $result_upload['raw_name']);
		$this->db->set('orig_name'     , $result_upload['orig_name']);
		$this->db->set('file_ext'      , $result_upload['file_ext']);
		$this->db->set('file_size'     , $result_upload['file_size']);
		$this->db->set('is_image'      , $result_upload['is_image'] == 1 ? 'Y' : 'N');
		$this->db->set('image_width'   , $result_upload['image_width']);
		$this->db->set('image_height'  , $result_upload['image_height']);
		$this->db->set('image_type'    , $result_upload['image_type']);
		$this->db->set('image_size_str', $result_upload['image_size_str']);
		$this->db->insert('tb_member_file') or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}
	
	/**
	 * 파일정보 삭제
	 */
	function delete_member_file($idx) {
		log_message('debug',"#### Change_model::delete_member_file($idx)");
		
		$member_file = $this->get_member_file($idx);
		if (empty($member_file['file_name'])) {
			return TRUE;
		}
		
		unlink($member_file['file_path'].$member_file['file_name']);
		unlink($member_file['file_path'].$member_file['raw_name']."_36".$member_file['file_ext']);
		unlink($member_file['file_path'].$member_file['raw_name']."_50".$member_file['file_ext']);
		unlink($member_file['file_path'].$member_file['raw_name']."_60".$member_file['file_ext']);
		unlink($member_file['file_path'].$member_file['raw_name']."_89".$member_file['file_ext']);

		$this->db->where('idx', $idx);
		$this->db->delete('tb_member_file') or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}

	/**
	 * 회원 사진을 가져온다.
	 */
	function get_member_file($idx) {
		log_message('debug',"#### Change_model::get_member_file($idx)");
		
		$query = $this->db->get_where('tb_member_file', array('idx'=>$idx)) or $this->logsql->log_sql('error');
		$result = $query->row_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 회원 사진의 유무를 알아온다.
	 */
	function is_already_member_file($user_id) {
		log_message('debug',"#### Change_model::is_already_member_file($user_id)");
		
		$this->db->where('user_id', $user_id);
		$this->db->from('tb_member_file');
		$result = $this->db->count_all_results();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		if ($result > 0) {
			return TRUE;
		}
        return FALSE;
	}

}

/* End of file change_model.php */
/* Location: ./application/models/mypage/change_model.php */